package com.health.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.system.common.exception.IdNotExistsException;
import com.health.system.common.exception.PasswordMatchException;
import com.health.system.common.utils.UserAuthUtil;
import com.health.system.dto.AdminUserListPageDTO;
import com.health.system.entity.UserAuth;
import com.health.system.entity.UserInfo;
import com.health.system.mapper.RoleMapper;
import com.health.system.mapper.UserAuthMapper;
import com.health.system.mapper.UserInfoMapper;
import com.health.system.service.UserAuthService;
import com.health.system.vo.ModifyUserPasswordVo;
import com.health.system.vo.ModifyUserVo;
import com.health.system.vo.UserListPageSearchVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class UserAuthServiceImpl extends ServiceImpl<UserAuthMapper, UserAuth> implements UserAuthService {
    @Autowired
    private UserAuthMapper userAuthMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void updatePassword(ModifyUserPasswordVo vo) {
        if (!StringUtils.equals(vo.getNewPassword(), vo.getConfirmPassword())) {
            throw new PasswordMatchException("确认密码错误");
        }

        Long id = UserAuthUtil.authId();
        UserAuth userAuth = userAuthMapper.selectById(id);
        if (ObjectUtils.isNull(userAuth)) {
            throw new IdNotExistsException(id);
        }

        boolean oldPasswordMatch = StringUtils.equals(userAuth.getPassword(), "{noop}" + vo.getOldPassword());
        if (!oldPasswordMatch) {
            throw new PasswordMatchException("旧密码不匹配");
        }

        UserAuth auth = UserAuth.builder()
                .id(id)
                .password("{noop}" + vo.getNewPassword())
                .build();
        userAuthMapper.updateById(auth);

    }

    @Override
    public IPage<AdminUserListPageDTO> listPage(Long current, Long size, UserListPageSearchVo condition) {
        IPage<AdminUserListPageDTO> page = new Page<>(current, size);
        return userAuthMapper.selectListPage(page, condition);
    }

    @Transactional
    @Override
    public void updateById(ModifyUserVo user) {
        UserInfo info = UserInfo.builder()
                .nickname(user.getNickName())
                .build();
        userInfoMapper.update(info, new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserId, user.getId()));

        roleMapper.deleteRelationUser(user.getId());
        user.getRoleIds().forEach(id -> roleMapper.insertRelationUser(id, user.getId()));
    }
}
